package io.aircraft.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.ConvertUtils;
import io.aircraft.modules.sys.dto.SysUserDTO;
import io.aircraft.modules.sys.service.SysUserService;
import io.aircraft.modules.wms.dao.StockDao;
import io.aircraft.modules.wms.dto.StockDTO;
import io.aircraft.modules.wms.dto.StockDetailDTO;
import io.aircraft.modules.wms.entity.StockDetailEntity;
import io.aircraft.modules.wms.entity.StockEntity;
import io.aircraft.common.service.impl.CrudServiceImpl;
import io.aircraft.modules.wms.entity.StockUsableEntity;
import io.aircraft.modules.wms.service.StockService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * ${comments}
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-10-13
 */
@Service
public class StockServiceImpl extends CrudServiceImpl<StockDao, StockEntity, StockDTO> implements StockService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageData<StockDTO> page(Map<String, Object> params) {
        IPage<StockEntity> page = baseDao.selectPage(
                getPage(params, "stock_id", true),
                getWrapper(params)
        );
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        //将用户编号填充至creatorName&updaterName
        page.getRecords().forEach(entity -> {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
        });
        return getPageData(page, StockDTO.class);
    }

    @Override
    public QueryWrapper<StockEntity> getWrapper(Map<String, Object> params) {
        String stockId = (String) params.get("stockId");
        String contNo = (String) params.get("contNo");
        String curPosition = (String) params.get("curPosition");
        String spuNo = (String) params.get("spuNo");
        String lotNo = (String) params.get("lotNo");
        String stockSt = (String) params.get("stockSt");
        String lockSt = (String) params.get("lockSt");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");

        QueryWrapper<StockEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(stockId), "stock_id", stockId);
        wrapper.eq(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        wrapper.eq(StringUtils.isNotBlank(curPosition), "cur_position", curPosition);
        wrapper.eq(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        wrapper.eq(StringUtils.isNotBlank(lotNo), "lot_no", lotNo);
        wrapper.eq(StringUtils.isNotBlank(stockSt), "stock_st", stockSt);
        wrapper.eq(StringUtils.isNotBlank(lockSt), "lock_st", lockSt);
        wrapper.ge(StringUtils.isNotBlank(startDate), "create_date", startDate);
        wrapper.le(StringUtils.isNotBlank(endDate), "create_date", endDate);

        return wrapper;
    }

    @Override
    public void unfreezeStockByTime() {
        baseDao.unfreezeStock();
    }

    @Override
    public StockDetailDTO getStockDetailsBySpu(Map<String, Object> params) {
        String spuNo = (String) params.get("spuNo");
        String lotNo = (String) params.get("lotNo");
        final StockDetailDTO dto = new StockDetailDTO();
        dto.setSpuNo(spuNo);
        dto.setLotNo(lotNo);
        //获取库存数量
        String stockQnty = baseDao.getStockBySpu(spuNo, lotNo);
        if (StringUtils.isBlank(stockQnty)) {
            stockQnty = "0";
        }
        //获取可用库存
        String usableQnty = baseDao.getUsableStockBySpu(spuNo, lotNo);
        if (StringUtils.isBlank(usableQnty)) {
            usableQnty = "0";
        }
        dto.setStockQnty(stockQnty);
        dto.setUsableQnty(usableQnty);
        return dto;
    }

    @Override
    public StockDetailDTO getStockDetails() {
        StockDetailEntity entity = baseDao.getStockDetails();
        return ConvertUtils.sourceToTarget(entity, StockDetailDTO.class);
    }

    @Override
    public List<StockDetailDTO> getStockDetailsGroupBySpu() {
        List<StockDetailEntity> entityList = baseDao.getStockDetailsGroupBySpu();

        return ConvertUtils.sourceToTarget(entityList, StockDetailDTO.class);
    }

    @Override
    public String getUsableStockByDvc(StockUsableEntity params) {
        final String usableStock = baseDao.getUsableStockByDvc(params);
        return usableStock;
    }

    @Override
    public List<StockEntity> getUsableStockListByDvc(StockUsableEntity params) {
        return baseDao.getUsableStockListByDvc(params);
    }
}